<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="RTU" prop="rtuId">
      <el-input v-model="dataForm.rtuId" placeholder="RTU"></el-input>
    </el-form-item>
    <el-form-item label="田块" prop="farmlandInfoId">
      <el-input v-model="dataForm.farmlandInfoId" placeholder="田块"></el-input>
    </el-form-item>
    <el-form-item label="基地" prop="deptId">
      <el-input v-model="dataForm.deptId" placeholder="基地"></el-input>
    </el-form-item>
    <el-form-item label="报文状态" prop="status">
      <el-input v-model="dataForm.status" placeholder="报文状态 0：待处理 1：待发送 2：已发送"></el-input>
    </el-form-item>
    <el-form-item label="遥测站号" prop="zhanghao">
      <el-input v-model="dataForm.zhanghao" placeholder="遥测站号"></el-input>
    </el-form-item>
    <el-form-item label="中心站号" prop="zhongxinghao">
      <el-input v-model="dataForm.zhongxinghao" placeholder="中心站号"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="password">
      <el-input v-model="dataForm.password" placeholder="密码"></el-input>
    </el-form-item>
    <el-form-item label="报文特征" prop="baowentype">
      <el-input v-model="dataForm.baowentype" placeholder="报文特征"></el-input>
    </el-form-item>
    <el-form-item label="流水号" prop="liushuihao">
      <el-input v-model="dataForm.liushuihao" placeholder="流水号"></el-input>
    </el-form-item>
    <el-form-item label="发报时间" prop="fabaoshijian">
      <el-input v-model="dataForm.fabaoshijian" placeholder="发报时间"></el-input>
    </el-form-item>
    <el-form-item label="闸门1" prop="gateA">
      <el-input v-model="dataForm.gateA" placeholder="闸门1"></el-input>
    </el-form-item>
    <el-form-item label="闸门2" prop="gateB">
      <el-input v-model="dataForm.gateB" placeholder="闸门2"></el-input>
    </el-form-item>
    <el-form-item label="闸门3" prop="gateC">
      <el-input v-model="dataForm.gateC" placeholder="闸门3"></el-input>
    </el-form-item>
    <el-form-item label="闸门4" prop="gateD">
      <el-input v-model="dataForm.gateD" placeholder="闸门4"></el-input>
    </el-form-item>
    <el-form-item label="闸门5" prop="gateE">
      <el-input v-model="dataForm.gateE" placeholder="闸门5"></el-input>
    </el-form-item>
    <el-form-item label="定时报间隔" prop="dsbjg">
      <el-input v-model="dataForm.dsbjg" placeholder="定时报间隔"></el-input>
    </el-form-item>
    <el-form-item label="日起始时间" prop="rqqssj">
      <el-input v-model="dataForm.rqqssj" placeholder="日起始时间"></el-input>
    </el-form-item>
    <el-form-item label="水位基值" prop="swjz">
      <el-input v-model="dataForm.swjz" placeholder="水位基值"></el-input>
    </el-form-item>
    <el-form-item label="水位上限" prop="swsx">
      <el-input v-model="dataForm.swsx" placeholder="水位上限"></el-input>
    </el-form-item>
    <el-form-item label="水位下限" prop="swxx">
      <el-input v-model="dataForm.swxx" placeholder="水位下限"></el-input>
    </el-form-item>
    <el-form-item label="发送报文" prop="messageCode">
      <el-input v-model="dataForm.messageCode" placeholder="发送报文"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          rtuId: '',
          farmlandInfoId: '',
          deptId: '',
          status: '',
          zhanghao: '',
          zhongxinghao: '',
          password: '',
          baowentype: '',
          liushuihao: '',
          fabaoshijian: '',
          gateA: '',
          gateB: '',
          gateC: '',
          gateD: '',
          gateE: '',
          dsbjg: '',
          rqqssj: '',
          swjz: '',
          swsx: '',
          swxx: '',
          messageCode: '',
        },
        dataRule: {
          rtuId: [
            { required: true, message: 'RTUid不能为空', trigger: 'blur' }
          ],
          farmlandInfoId: [
            { required: true, message: '田块id不能为空', trigger: 'blur' }
          ],
          deptId: [
            { required: true, message: '部门（基地）id不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '报文状态 0：待处理 1：待发送 2：已发送不能为空', trigger: 'blur' }
          ],
          zhanghao: [
            { required: true, message: '遥测站号不能为空', trigger: 'blur' }
          ],
          zhongxinghao: [
            { required: true, message: '中心站号不能为空', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '密码不能为空', trigger: 'blur' }
          ],
          baowentype: [
            { required: true, message: '报文特征不能为空', trigger: 'blur' }
          ],
          liushuihao: [
            { required: true, message: '流水号不能为空', trigger: 'blur' }
          ],
          fabaoshijian: [
            { required: true, message: '发报时间不能为空', trigger: 'blur' }
          ],
          gateA: [
            { required: true, message: '闸门1不能为空', trigger: 'blur' }
          ],
          gateB: [
            { required: true, message: '闸门2不能为空', trigger: 'blur' }
          ],
          gateC: [
            { required: true, message: '闸门3不能为空', trigger: 'blur' }
          ],
          gateD: [
            { required: true, message: '闸门4不能为空', trigger: 'blur' }
          ],
          gateE: [
            { required: true, message: '闸门5不能为空', trigger: 'blur' }
          ],
          dsbjg: [
            { required: true, message: '定时报间隔不能为空', trigger: 'blur' }
          ],
          rqqssj: [
            { required: true, message: '日起始时间不能为空', trigger: 'blur' }
          ],
          swjz: [
            { required: true, message: '水位基值不能为空', trigger: 'blur' }
          ],
          swsx: [
            { required: true, message: '水位上限不能为空', trigger: 'blur' }
          ],
          swxx: [
            { required: true, message: '水位下限不能为空', trigger: 'blur' }
          ],
          messageCode: [
            { required: true, message: '发送报文不能为空', trigger: 'blur' }
          ],       
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/buss/bussrtusendrecord/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.rtuId = data.bussrtusendrecord.rtuId
                this.dataForm.farmlandInfoId = data.bussrtusendrecord.farmlandInfoId
                this.dataForm.deptId = data.bussrtusendrecord.deptId
                this.dataForm.status = data.bussrtusendrecord.status
                this.dataForm.zhanghao = data.bussrtusendrecord.zhanghao
                this.dataForm.zhongxinghao = data.bussrtusendrecord.zhongxinghao
                this.dataForm.password = data.bussrtusendrecord.password
                this.dataForm.baowentype = data.bussrtusendrecord.baowentype
                this.dataForm.liushuihao = data.bussrtusendrecord.liushuihao
                this.dataForm.fabaoshijian = data.bussrtusendrecord.fabaoshijian
                this.dataForm.gateA = data.bussrtusendrecord.gateA
                this.dataForm.gateB = data.bussrtusendrecord.gateB
                this.dataForm.gateC = data.bussrtusendrecord.gateC
                this.dataForm.gateD = data.bussrtusendrecord.gateD
                this.dataForm.gateE = data.bussrtusendrecord.gateE
                this.dataForm.dsbjg = data.bussrtusendrecord.dsbjg
                this.dataForm.rqqssj = data.bussrtusendrecord.rqqssj
                this.dataForm.swjz = data.bussrtusendrecord.swjz
                this.dataForm.swsx = data.bussrtusendrecord.swsx
                this.dataForm.swxx = data.bussrtusendrecord.swxx
                this.dataForm.messageCode = data.bussrtusendrecord.messageCode              
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/buss/bussrtusendrecord/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'rtuId': this.dataForm.rtuId,
                'farmlandInfoId': this.dataForm.farmlandInfoId,
                'deptId': this.dataForm.deptId,
                'status': this.dataForm.status,
                'zhanghao': this.dataForm.zhanghao,
                'zhongxinghao': this.dataForm.zhongxinghao,
                'password': this.dataForm.password,
                'baowentype': this.dataForm.baowentype,
                'liushuihao': this.dataForm.liushuihao,
                'fabaoshijian': this.dataForm.fabaoshijian,
                'gateA': this.dataForm.gateA,
                'gateB': this.dataForm.gateB,
                'gateC': this.dataForm.gateC,
                'gateD': this.dataForm.gateD,
                'gateE': this.dataForm.gateE,
                'dsbjg': this.dataForm.dsbjg,
                'rqqssj': this.dataForm.rqqssj,
                'swjz': this.dataForm.swjz,
                'swsx': this.dataForm.swsx,
                'swxx': this.dataForm.swxx,
                'messageCode': this.dataForm.messageCode,               
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
